<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Model\Users;

class SmartController extends Controller
{
    public function login(Request $request){
        if (isset($_COOKIE['is_login'])) {
            header('location: /index');
            exit;
        }
        return view('smart/login');
    }
    public function index(Request $request){
        if (!isset($_COOKIE['is_login'])) {
            header('location: /login');
            exit;
        }
        return view('smart/index');
    }
    public function dologin(Request $request){
        $name = isset($request->name)?$request->name:'';
        $tel = isset($request->tel)?$request->tel:'';
        $success = false;
        $msg = '';
        if($name == '' || $tel == ''){
            $msg = "姓名或手机号不能为空";
        }else{
            $count = Users::where(['name'=>$name,'tel'=>$tel])->count();
            if($count <= 0){
                $msg = "姓名或手机号填写错误";
            }else{
                setcookie("is_login",'1',time()+315360000,'/');
                $msg = "登录成功";
                $success = true;
            }
        }
        $data['success'] = $success;
        $data['msg'] = $msg;
        echo json_encode($data);
    }
}